package com.libre.workflow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libre.common.base.BaseController;
import com.libre.common.utils.ResultUtil;
import com.libre.common.vo.PageVo;
import com.libre.common.vo.Result;
import com.libre.workflow.service.MyInstanceService;
import com.libre.workflow.vo.MyInstanceAddVo;
import com.libre.workflow.vo.MyInstanceStateVo;
import com.libre.workflow.vo.MyInstanceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description 流程实例
 * @author 庞留杰
 * @date 2020/9/14 9:38
 * @version V1.0
 **/
@Slf4j
@RestController
@RequestMapping("/workflow/instance")
public class MyInstanceController extends BaseController {

    @Autowired
    private MyInstanceService instanceService;

    /**
     * 功能描述: 启动：提交一个申请单
     *   启动流程实例(提交一个申请单)，实例启动成功，返回当前活动任务
     * @author: 庞留杰
     * @exception
     * @date: 2020/9/11 9:17
     */
    @PostMapping("/startByKey")
    public Result startByKey(@RequestBody MyInstanceAddVo instanceAddVo) {
        String processInstanceId = instanceService.startByKey(this.getLoginUser(), instanceAddVo);
        //通过processInstance.getId() 可以判断流程是否发起成功
        return new ResultUtil<>().setData(processInstanceId);
    }

    /**
     * 功能描述: 分页查询流程实例【操作表：act_ru_execution】
     * @author: 庞留杰
     * @param instanceVo
     * @return:
     * @exception
     * @date: 2020/9/14 9:44
     */
    @GetMapping("/findPage")
    public Result findPage(@ModelAttribute PageVo pageVo, @ModelAttribute MyInstanceVo instanceVo) {
        Page page = instanceService.findPage(pageVo, instanceVo);
        return new ResultUtil<>().setData(page);
    }

    /**
     * 功能描述: 流程的挂起、激活
     * @author: 庞留杰
     * @param instanceStateVo
     * @return: Result
     * @exception
     * @date: 2020/9/14 9:53
     */
    @PostMapping("/editState")
    public Result editState(@RequestBody MyInstanceStateVo instanceStateVo) {
        String resutl = instanceService.editState(instanceStateVo);
        return new ResultUtil<>().setSuccessMsg(resutl);
    }

    @PostMapping(value = "delete/{processInstanceId}")
    public Result delete(@PathVariable String processInstanceId) {
        instanceService.delete(processInstanceId);
        return new ResultUtil<>().setSuccessMsg("删除成功");
    }

}
